﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SdlDotNet.Graphics;
using SdlDotNet.Graphics.Sprites;
using Color = System.Drawing.Color;

namespace MapEditor
{
    public class Map
    {
        public string Name;
        public int ID;
        public int xPos;
        public int yPos;
        public string Filename;
        public List<Tile[,]> Layers = new List<Tile[,]>();
        public Surface DrawSurface = new Surface(240,160);

        public Map()
        {
            Tile[,] Layer = new Tile[15, 10];
            Tile t = new Tile();
            t.Image = Global.Tileset[0];
            t.ID = 0;

            for (int x = 0; x < 15; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    Layer[x, y] = t;
                }
            }

            Layers.Add(Layer);
        }

        public void Draw(int CurrLayer)
        {
            DrawSurface.Fill(Color.Black);

            int Layer = 0;

            foreach (Tile[,] Tilemap in Layers)
            {
                for (int x = 0; x < Tilemap.GetLength(0); x++)
                {
                    for (int y = 0; y < Tilemap.GetLength(1); y++)
                    {
                        if (Tilemap[x, y] != null)
                        {
                            int DeltaLayer = Math.Max(0, Layer - CurrLayer);
                            float AlphaCoef = 1f / (1f + (DeltaLayer * DeltaLayer));
                            //float AlphaCoef = 1.0f- (DeltaLayer*DeltaLayer)/(255*255);
                            Tilemap[x, y].Draw(x * 16, y * 16, DrawSurface, (byte)(255 * AlphaCoef));
                        }
                    }
                }

                DrawSurface.Update();

                Layer++;
            }
        }

        public string Unload()
        {
            string s = "";

            s += xPos+"|"+yPos+"\n";

            foreach (Tile[,] Tilemap in Layers)
            {
                for (int y = 0; y < Tilemap.GetLength(1); y++)
                {
                    for (int x = 0; x < Tilemap.GetLength(0); x++)
                    {
                        if (Tilemap[x, y] != null)
                        {
                            s += Convert.ToString(Tilemap[x, y].ID, 16);
                        }
                        s += "|";
                    }
                    s += "\n";
                }
            }

            return s;
        }

        public void Load(string filename)
        {
            Filename = filename.Split('.')[0];
            Layers.Clear();

            StreamReader reader = File.OpenText(filename);

            string[] line = reader.ReadLine().Split('|');

            xPos = Convert.ToInt32(line[0]);
            yPos = Convert.ToInt32(line[1]);

            while (!reader.EndOfStream)
            {
                Tile[,] Layer = new Tile[15, 10];

                for (int y = 0; y < Layer.GetLength(1); y++)
                {
                     line = reader.ReadLine().Split('|');

                    for (int x = 0; x < Layer.GetLength(0); x++)
                    {   
                        if (!String.IsNullOrWhiteSpace(line[x]))
                        {
                            Tile t = new Tile();
                            t.ID = Convert.ToInt32(line[x], 16);
                            t.Image = Global.Tileset[t.ID];
                            Layer[x, y] = t;
                        }
                    }
                }

                Layers.Add(Layer);
            }

            reader.Close();
        }
    }
}
